
package com.aurora.oa.work.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.aurora.oa.Constants.AuroraConstants;
import com.aurora.oa.R;
import com.aurora.oa.message.bean.ContactsInfo;
import com.aurora.oa.work.bean.CompanyStructureBean;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.List;


/**
 * Created by Administrator on 2017/3/30 0030.
 */

public class PersonSelectAdapter extends RecyclerView.Adapter<PersonSelectAdapter.MViewHolder> {

    private static final int TYPE_ADD = 1;
    private static final int TYPE_HEAD_ITEM = 2;
    private Context context;
    private LayoutInflater mInflater;
    private List<CompanyStructureBean.ChildNodeBean> ItemDatas;
    private OnItemClickListener onItemClickListener;
    @Override
    public MViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MViewHolder viewHolder = new MViewHolder(mInflater.inflate(
                R.layout.item_grid_contacts, parent, false));
        return viewHolder;
    }

    public void setOnClickListener(OnItemClickListener onClickListener){
        onItemClickListener = onClickListener;
    }

    @Override
    public void onBindViewHolder(MViewHolder holder,  int position) {
        if(holder.getItemViewType() ==TYPE_ADD){
            holder.sdv_mhead_img.setImageResource(R.mipmap.ico_btn_add_people);
            holder.tv_user_name.setText("添加人员");
        }else{
            holder.sdv_mhead_img.setImageURI(AuroraConstants.BASE_SERVER_URL+ItemDatas.get(position).getHead().getPath());
            holder.tv_user_name.setText(ItemDatas.get(position).getName());
        }

        holder.itemView.setTag(holder.getLayoutPosition());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onItemClickListener.onItemClick(Integer.parseInt(view.getTag()+""));
            }
        });

    }

    public PersonSelectAdapter(Context context,List<CompanyStructureBean.ChildNodeBean> contactsBeen) {
        this.ItemDatas = contactsBeen;
        mInflater = LayoutInflater.from(context);
        this.context = context;
    }

    @Override
    public int getItemCount() {
        if (ItemDatas==null||ItemDatas.size()==0){
            return 1;
        }
        return ItemDatas.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (ItemDatas==null||ItemDatas.size()==0){
            return TYPE_ADD;
        }else{
            return TYPE_HEAD_ITEM;
        }
    }

    class MViewHolder extends RecyclerView.ViewHolder{
        private TextView tv_user_name;
        private SimpleDraweeView sdv_mhead_img;
        public MViewHolder(View itemView) {
            super(itemView);
            tv_user_name = (TextView) itemView.findViewById(R.id.tv_user_name);
            sdv_mhead_img = (SimpleDraweeView) itemView.findViewById(R.id.sdv_mhead_img);
        }
    }

    public interface OnItemClickListener{
        void onItemClick(int position);
    }
}

